home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archives / GNU / WinGnuPlot22.lha / WinGnuPlot / Source / OutlineFont.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-09  |  6.0 KB  |  287 lines

  1. /* this defines some routines for handling of Outline-Fonts */
  2.  
  3. #include "struct.c"
  4. #include "OutlineFont.h"
  5.  
  6. #define CW 80
  7. #define CH 35
  8.  
  9. static int   cwidth, cheight;
  10. static ULONG XYdpi, PointSize;
  11.  
  12. struct Glyph
  13. {
  14.  int    top, left, width, height;
  15.  int    basex, basey;
  16.  int    bmsize, bpr;
  17.  UBYTE *bitmap;
  18. };
  19.  
  20. static struct Glyph *HGlyphs[256], *VGlyphs[256];
  21.  
  22.        struct Library     *BulletBase   = NULL;
  23. static struct GlyphEngine *BulletEngine = NULL;
  24.  
  25.  
  26. static BOOL IsOutlineFont(char *fontname)
  27. {
  28.  BPTR                       FontsDir, Font;
  29.  char                       FullFontName[255];
  30.  BOOL                       h = FALSE;
  31.  struct FontContentsHeader *fch;
  32.  
  33.  /* trick to get the real fonts-directory incase of MultiAssign !! */
  34.  strcpy(FullFontName, "FONTS:");
  35.  AddPart(FullFontName, fontname, 255);
  36.  if (Font = Lock(FullFontName, ACCESS_READ))
  37.   {
  38.    FontsDir = ParentDir(Font);
  39.    if (fch = NewFontContents(FontsDir, fontname))
  40.     {
  41.      if (fch->fch_FileID == OFCH_ID)
  42.       h = TRUE;
  43.      DisposeFontContents(fch);
  44.     }
  45.    UnLock(FontsDir);
  46.    UnLock(Font);
  47.   }
  48.  
  49.  return h;
  50. }
  51.  
  52. static struct GlyphEngine *GetEngine(char *fontname, struct TagItem **OTags)
  53. {
  54.  BPTR                f;
  55.  int                 l;
  56.  char                enginename[80];
  57.  struct TagItem     *Tags, *ti, *tstate;
  58.  struct GlyphEngine *Engine = NULL;
  59.  
  60.  if (f = Open(fontname, MODE_OLDFILE))
  61.   {
  62.    Seek(f, 0, OFFSET_END);
  63.    l = Seek(f, 0, OFFSET_BEGINNING);
  64.    if (Tags = malloc(l))
  65.     {
  66.      Read(f, Tags, l);
  67.  
  68.      tstate = Tags;
  69.      while (ti = NextTagItem(&tstate))
  70.       if (ti->ti_Tag & OT_Indirect)
  71.        ti->ti_Data += (ULONG)Tags;
  72.      
  73.      if ((ti = FindTagItem(OT_FileIdent, Tags)) && ti->ti_Data == l)
  74.       {
  75.        if (ti = FindTagItem(OT_Engine, Tags))
  76.     {
  77.      strcpy(enginename, (char *)(ti->ti_Data));
  78.      strcat(enginename, ".library");
  79.      if (BulletBase = OpenLibrary(enginename, 0))
  80.       {
  81.        Engine = OpenEngine();
  82.        *OTags = Tags;
  83.       }
  84.     }
  85.       }
  86.     }
  87.    Close(f);
  88.   }
  89.  
  90.  return Engine;
  91. }
  92.  
  93. static struct Glyph *GetGlyph(char ch, BOOL Vertical)
  94. {
  95.  struct GlyphMap  *GlyphMap;
  96.  struct Glyph    **glyphs;
  97.  
  98.  glyphs = (Vertical) ? VGlyphs : HGlyphs;
  99.  if (!glyphs[ch])
  100.   {
  101.    glyphs[ch] = malloc(sizeof(struct Glyph));
  102.    SetInfo(BulletEngine,
  103.        OT_GlyphCode, ch,
  104.        OT_RotateSin,   (Vertical) ? 0x00010000 : 0x00000000,
  105.        OT_RotateCos,   (Vertical) ? 0x00000000 : 0x00010000,
  106.        OT_PointHeight, PointSize,
  107.        OT_DeviceDPI,   XYdpi,
  108.        OT_DotSize,     0x00640064,
  109.        TAG_END);
  110.    if (ObtainInfo(BulletEngine,
  111.           OT_GlyphMap, &GlyphMap,
  112.           TAG_END) == OTERR_Success)
  113.     {
  114.      glyphs[ch]->top = GlyphMap->glm_BlackTop;
  115.      glyphs[ch]->left = GlyphMap->glm_BlackLeft;
  116.      glyphs[ch]->height = GlyphMap->glm_BlackHeight+1;
  117.      glyphs[ch]->width = GlyphMap->glm_BlackWidth+1;
  118.      glyphs[ch]->basey = GlyphMap->glm_Y0-GlyphMap->glm_BlackTop;
  119.      glyphs[ch]->basex = GlyphMap->glm_X0-GlyphMap->glm_BlackLeft;
  120.      glyphs[ch]->bpr = GlyphMap->glm_BMModulo;
  121.      glyphs[ch]->bmsize = GlyphMap->glm_BMModulo*GlyphMap->glm_BMRows;
  122.      if (glyphs[ch]->bitmap = AllocMem(glyphs[ch]->bmsize, MEMF_CHIP))
  123.       CopyMemQuick(GlyphMap->glm_BitMap, glyphs[ch]->bitmap, glyphs[ch]->bmsize);
  124.      ReleaseInfo(BulletEngine,
  125.          OT_GlyphMap, GlyphMap,
  126.          TAG_END);
  127.     }
  128.    else
  129.     {
  130.      glyphs[ch]->top = 0;
  131.      glyphs[ch]->left = 0;
  132.      glyphs[ch]->height = cheight;
  133.      glyphs[ch]->width = cwidth;
  134.      glyphs[ch]->basey = 0;
  135.      glyphs[ch]->basex = 0;
  136.      glyphs[ch]->bpr = (cwidth/32 + 1) * 4;
  137.      glyphs[ch]->bmsize = glyphs[ch]->bpr * cheight;
  138.      glyphs[ch]->bitmap = AllocMem(glyphs[ch]->bmsize, MEMF_CHIP|MEMF_CLEAR);
  139.     }
  140.   }
  141.  
  142.  
  143.  return glyphs[ch];
  144. }
  145.  
  146. static void FreeGlyph(struct Glyph *glyph)
  147. {
  148.  FreeMem(glyph->bitmap, glyph->bmsize);
  149. }
  150.  
  151. static void FreeGlyphs(void)
  152. {
  153.  int ch;
  154.  
  155.  for(ch=0; ch <= 255; ch++)
  156.   {
  157.    if (HGlyphs[ch])
  158.     {
  159.      FreeGlyph(HGlyphs[ch]);
  160.      free(HGlyphs[ch]);
  161.      HGlyphs[ch] = NULL;
  162.     }
  163.    if (VGlyphs[ch])
  164.     {
  165.      FreeGlyph(VGlyphs[ch]);
  166.      free(VGlyphs[ch]);
  167.      VGlyphs[ch] = NULL;
  168.     }
  169.   }
  170. }
  171.  
  172. BOOL OpenOutlineFont(char *font)
  173. {
  174.  char            fontname[80], otagname[80];
  175.  struct TagItem *OTags;
  176.  int             ch;
  177.  
  178.  strcpy(fontname, font); strcat(fontname, ".font");
  179.  if (IsOutlineFont(fontname))
  180.   {
  181.    strcpy(otagname, "FONTS:"); strcat(otagname, font); strcat(otagname, ".otag");
  182.    if (BulletEngine = GetEngine(otagname, &OTags))
  183.     {
  184.      strcpy(fontname, "FONTS:"); strcat(fontname, font); strcat(fontname, ".font");
  185.      SetInfo(BulletEngine,
  186.          OT_OTagPath, fontname,
  187.          OT_OTagList, OTags,
  188.          TAG_END);
  189.  
  190.      for(ch=0; ch <= 255; ch++)
  191.       HGlyphs[ch] = VGlyphs[ch] = NULL;
  192.  
  193.      return TRUE;
  194.     }
  195.   }
  196.  return FALSE;
  197. }
  198.  
  199. void CloseOutlineFont(void)
  200. {
  201.  FreeGlyphs();
  202.  if (BulletEngine) CloseEngine(BulletEngine);
  203.  if (BulletBase)   CloseLibrary(BulletBase);
  204. }
  205.  
  206. void PrintString(struct RastPort *rp, int x0, int y0, char *s, BOOL Vertical)
  207. {
  208.  char         *ch;
  209.  int           x, y;
  210.  struct Glyph *glyph;
  211.  
  212.  x = x0; y = y0;
  213.  for(ch = s; *ch; ch++)
  214.   {
  215.    glyph = GetGlyph(*ch, Vertical);
  216.    BltTemplate(glyph->bitmap+glyph->top*glyph->bpr,
  217.            glyph->left, glyph->bpr,
  218.            rp,
  219.            x-glyph->basex, 
  220.            y-glyph->basey,
  221.            glyph->width, glyph->height);
  222.    WaitBlit();
  223.    if (Vertical)
  224.     y -= glyph->height;
  225.    else
  226.     x += glyph->width;
  227.   }
  228. }
  229.  
  230. void SetFontSize(int w, int h)
  231. {
  232.  static int last_x = 0, last_y = 0, last_p = 0;
  233.  int x, y, p;
  234.  
  235.  cwidth = w/CW; cheight = h/CH;
  236.  
  237.  if (w*CH < h*CW)
  238.   {
  239.    x = 100;
  240.    y = (100 * h * CW) / (w * CH);
  241.    p = (w * 65536L) / CW;
  242.   }
  243.  else
  244.   {
  245.    y = 100;
  246.    x = (100 * w * CH) / (h * CW);
  247.    p = (h * 65536L) / CH;
  248.   }
  249.  
  250.  PointSize = p;
  251.  XYdpi = x<<16 | y;
  252.  
  253.  if (last_x != x || last_y != y || last_p != PointSize)
  254.   {
  255.    FreeGlyphs();
  256.    last_x = x;
  257.    last_y = y;
  258.    last_p = PointSize;
  259.   }
  260. }
  261.  
  262. int TextLen(char *s)
  263. {
  264.  char *ch;
  265.  int   txtlen = 0;
  266.  
  267.  for(ch = s; *ch; ch++)
  268.   txtlen += (GetGlyph(*ch, FALSE))->width + 1;
  269.  
  270.  return txtlen;
  271. }
  272.  
  273. int _STI_10000_InitGlyphs(void)
  274. {
  275.  int ch;
  276.  
  277.  for(ch=0; ch <= 255; ch++)
  278.   HGlyphs[ch] = VGlyphs[ch] = NULL;
  279.  
  280.  return 0;
  281. }
  282.  
  283. void _STD_10000_CloseOutlineFonts(void)
  284. {
  285.  CloseOutlineFont();
  286. }
  287.